package cn.wolfcode.p2p.web.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BidConst;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.BidRequestAuditHistory;
import cn.wolfcode.p2p.business.query.BidRequestQuery;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.vo.PublishAuditVO;
import com.sun.org.apache.bcel.internal.generic.IUSHR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BidRequestAuditController {
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRealAuthService realAuthService;

    // 发标前审核列表
    @RequestMapping("/bidrequest_publishaudit_list")
    public String publishAuditList(Model model, @ModelAttribute("qo") BidRequestQuery qo) {
//        qo.setBidRequestState(BidConst.BIDREQUEST_STATE_APPLY);
        model.addAttribute("pageInfo", bidRequestService.query(qo));
        return "bidrequest/publish_audit";
    }

    // 审核操作
    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public JSONResult publishAudit(Long id, int state, String remark, String publishTime) {
        bidRequestService.publishAudit(id, state, remark, publishTime);
        return new JSONResult();
    }

    // 查看借款的详情
    @RequestMapping("/borrow_info")
    public String borrowInfo(Model model, Long id) {
        BidRequest bidRequest = bidRequestService.get(id);
        UserInfo applier = userInfoService.get(bidRequest.getCreateUser().getId());
        model.addAttribute("userInfo", applier);
        model.addAttribute("bidRequest", bidRequestService.get(id));
        List<BidRequestAuditHistory> auditHistories = bidRequestService.listAuditHistorysByBidRequestId(id);
        if (auditHistories != null) {
            model.addAttribute("audits", auditHistories);
        } else {
            model.addAttribute("audits", new ArrayList<>());
        }
        model.addAttribute("realAuth", realAuthService.get(applier.getRealAuthId()));
        return "bidrequest/borrow_info";
    }

    //=====================================================


}
